home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / tex / dvi / dvipssrc.zoo / repack.c < prev    next >
C/C++ Source or Header  |  1990-12-29  |  15KB  |  476 lines

  1. /*
  2.  *   Compressed TeX fonts in PostScript.  Copyright (C) 1989
  3.  *   by Radical Eye Software.  All Rights Reserved.
  4.  *   (Slight mods by Don Knuth in December 89.)
  5.  */
  6. #include "structures.h" /* The copyright notice in that file is included too! */
  7. #ifdef DEBUG
  8. extern integer debug_flag;
  9. #endif /* DEBUG */
  10.  
  11. /*   Given a raster that has been unpacked from PK format,
  12.  *   we compress it by another scheme that is suitable for
  13.  *   a PostScript-oriented unpacking. We write instructions for
  14.  *   a little interpreter whose one-byte instructions have the form
  15.  *   18*opcode+parameter. The interpreter forms each row based
  16.  *   on simple transformations to the previous row. */
  17.  
  18. #define MAXOUT (18)
  19. #define CMD(n) (MAXOUT*(n))
  20. #define ADVXCHG1 CMD(0)
  21. #define ADVXCHG1END CMD(1)
  22. #define CHGX CMD(2)-1
  23. #define CHGXEND CMD(3)-1
  24. #define ADVXLSH CMD(4)
  25. #define ADVXLSHEND CMD(5)
  26. #define ADVXRSH CMD(6)
  27. #define ADVXRSHEND CMD(7)
  28. #define ADVX CMD(8)-1
  29. #define REPX CMD(9)-1
  30. #define SETX CMD(10)-1
  31. #define CLRX CMD(11)-1
  32. #define ADVXCHG2 CMD(12)
  33. #define ADVXCHG2END CMD(13)
  34. #define END CMD(14)
  35.  
  36. extern char *malloc() ;
  37. extern free() ;
  38. extern void error() ;
  39. extern long getlong() ;
  40. extern long unpack() ;
  41.  
  42. static int rowlength = 0 ;
  43. static unsigned char *specdata ;
  44. static long tslen = 0 ;
  45. static unsigned char *tempstore, *tsp, *tsend ;
  46.  
  47. void putlong(a, i)
  48. register char *a ;
  49. long i ;
  50. {
  51.    a[0] = i >> 24 ;
  52.    a[1] = i >> 16 ;
  53.    a[2] = i >> 8 ;
  54.    a[3] = i ;
  55. }
  56.  
  57. long getlong(a)
  58. register unsigned char *a ;
  59. {
  60.    return ((((((a[0] << 8L) + a[1]) << 8L) + a[2]) << 8L) + a[3]) ;
  61. }
  62.  
  63. /* First, a routine that appends one byte to the compressed output. */
  64.  
  65. #define addtse(n) {lcm=tsp-tempstore;addts(n);} /* mark END option position */
  66.  
  67. static void addts(what)
  68. register unsigned char what ;
  69. {
  70.    register unsigned char *p, *q ;
  71.  
  72.    if (tsp >= tsend) {
  73.       if (tempstore == NULL) {
  74.          tslen = 4096 ;
  75.          tempstore = (unsigned char *)malloc((unsigned)tslen) ;
  76.          if (tempstore == NULL)
  77.             error("! out of memory") ;
  78.          tsp = tempstore ;
  79.       } else {
  80.          tslen = 2 * tslen ;
  81.          tsp = (unsigned char *)malloc((unsigned)tslen) ;
  82.          if (tsp == NULL)
  83.             error("! out of memory") ;
  84.          for (p=tempstore, q=tsp; p<tsend; p++, q++)
  85.             *q = *p ;
  86.          free((char *)tempstore) ;
  87.          tempstore = tsp ;
  88.          tsp = q ;
  89.       }
  90.       tsend = tempstore + tslen ;
  91.    }
  92.    *tsp++ = what ;
  93. }
  94.  
  95. /* Next, a routine that discovers how to do the compression. */
  96.  
  97. #define rsh(a,b) ( ((a)==0) ? ((b)==128) : ( ((a)==255) ? ((b)==127) :\
  98.                                     ((b)==(((a)>>1)|((a)&128))) ))
  99. #define lsh(a,b) ( ((a)==0) ? ((b)==1) : ( ((a)==255) ? ((b)==254) :\
  100.                                     ((b)==((((a)<<1)&255)|((a)&1))) ))
  101. #define DIFFERENT (1)
  102. #define LSHPOSSIB (2)
  103. #define RSHPOSSIB (4)
  104. #define BLKPOSSIB (8)
  105. #define WHTPOSSIB (16)
  106. #define ENDROW (32)
  107. #define NOPOSSIB(n) ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0)
  108. #define NOSHIFT(n) ((n&(LSHPOSSIB|RSHPOSSIB))==0)
  109. /*
  110.  *   Our input bytes are packed to the 16-bit word.  On output,
  111.  *   they're packed to bytes. Thus, we may have to skip a byte at
  112.  *   the end of each row.
  113.  */
  114. void dochar(from, width, height)
  115. unsigned char *from ;
  116. short width, height ; /* in bytes */
  117. {
  118.    register int i ;
  119.    register unsigned char *f, *t, *d ;
  120.    register unsigned char *e ;
  121.    register int accum ;
  122.    int j, k, kk ;
  123.    int diffrow ;
  124.    int repeatcount ;
  125.    int lit, pos, cmd = 0 ;
  126.    long lcm ;
  127.    int widthc ;
  128.  
  129.    widthc = width + (width & 1) ; /* halfword correction */
  130.    lcm = -1 ;
  131.    if (widthc > rowlength) {
  132.       if (rowlength)
  133.          free((char *)specdata) ;
  134.       rowlength = widthc ;
  135.       specdata = (unsigned char *)malloc((unsigned)(rowlength + 15)) ;
  136.       if (specdata == NULL)
  137.          error("! out of memory") ;
  138.    }
  139.    for (i= -15, t=specdata; i<=widthc; i++, t++)
  140.       *t = 0 ;
  141.    repeatcount = 0 ;
  142.    f = specdata + 2 ;
  143.    for (j=0; j<height; j++, f = from, from += widthc) {
  144.       diffrow = 0 ;
  145.       for (i=0, t=from, d=specdata; i<width; i++, f++, t++, d++) {
  146.          if (*f == *t) {
  147.             if (*t == 0)
  148.                *d = WHTPOSSIB ;
  149.             else if (*t == 255)
  150.                *d = BLKPOSSIB ;
  151.             else
  152.                *d = 0 ;
  153.          } else {
  154.             accum = DIFFERENT ;
  155.             if (rsh(*f, *t))
  156.                accum |= RSHPOSSIB ;
  157.             else if (lsh(*f, *t))
  158.                accum |= LSHPOSSIB ;
  159.             if (*t == 0)
  160.                accum |= WHTPOSSIB ;
  161.             else if (*t == 255)
  162.                accum |= BLKPOSSIB ;
  163.             *d = accum ;
  164.             diffrow++ ;
  165.          }
  166.       } /* end 'for i' */
  167.       *d = ENDROW ;
  168.       if (diffrow == 0) {
  169.          repeatcount++ ;
  170.       } else {
  171.          if (repeatcount) {
  172.             while (repeatcount > MAXOUT) {
  173.                addts((unsigned char)(REPX+MAXOUT)) ;
  174.                repeatcount -= MAXOUT ;
  175.             }
  176.             addts((unsigned char)(REPX+repeatcount)) ;
  177.             repeatcount = 0 ;
  178.          }
  179.          pos = 0 ;
  180.          for (i=0, d=specdata, f=t-width; i<width;) {
  181.             if ((*d & DIFFERENT) == 0) {
  182.                i++ ;
  183.                d++ ;
  184.                f++ ;
  185.             } else {
  186.                accum = 0 ;
  187.                if (pos != i)
  188.                   lit = NOSHIFT(*d) ;
  189.                else /* N.B.: 'lit' does not imply literate programming here */
  190.                   lit = NOPOSSIB(*d) ;
  191.                for (e=d; ;e++) {
  192.                   if (NOPOSSIB(*e))
  193.                      lit = 1 ;
  194.                   if ((*e & DIFFERENT) == 0)
  195.                      break ;
  196.                   if ((*e & WHTPOSSIB) &&
  197.                       (e[1] & WHTPOSSIB)) {
  198.                      while (*e & WHTPOSSIB) {
  199.                         e++ ;
  200.                         accum++ ;
  201.                      }
  202.                      cmd = CLRX ;
  203.                      e -= accum ;
  204.                      break ;
  205.                   } else if ((*e & BLKPOSSIB) &&
  206.                       (e[1] & BLKPOSSIB)) {
  207.                      while (*e & BLKPOSSIB) {
  208.                         e++ ;
  209.                         accum++ ;
  210.                      }
  211.                      cmd = SETX ;
  212.                      e -= accum ;
  213.                      break ;
  214.                   }
  215.                } /* end 'for e'; d pts to first bad byte, e to next good one */
  216.                while (i - pos > MAXOUT) {
  217.                   addts((unsigned char)(ADVX+MAXOUT)) ;
  218.                   pos += MAXOUT ;
  219.                }
  220.                if (k = (e - d)) {
  221.                   if (lit) {
  222.                      if (k > 2) {
  223.                         if (i > pos) {
  224.                            addts((unsigned char)(ADVX + i - pos)) ;
  225.                            pos = i ;
  226.                         }
  227.                         while (k > MAXOUT) {
  228.                            addts((unsigned char)(CHGX + MAXOUT)) ;
  229.                            for (kk=0; kk<MAXOUT; kk++)
  230.                               addts((unsigned char)(*f++)) ;
  231.                            d += MAXOUT ;
  232.                            pos += MAXOUT ;
  233.                            i += MAXOUT ;
  234.                            k -= MAXOUT ;
  235.                         }
  236.                         addtse((unsigned char)(CHGX + k)) ;
  237.                         pos += k ;
  238.                         for (; d<e; d++, i++, f++)
  239.                            addts((unsigned char)(*f)) ;
  240.                      } else {
  241.                         if (k == 1) {
  242.                            if (i == pos+MAXOUT) {
  243.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  244.                               pos = i ;
  245.                            }
  246.                            addtse((unsigned char)(ADVXCHG1 + i - pos)) ;
  247.                            addts((unsigned char)(*f)) ;
  248.                            i++ ;
  249.                            pos = i ;
  250.                            d++ ;
  251.                            f++ ;
  252.                         } else {
  253.                            if (i == pos+MAXOUT) {
  254.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  255.                               pos = i ;
  256.                            }
  257.                            addtse((un